﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Bussiness;
using Bussiness.Service;

namespace Bussiness.View.BackEnd
{
    public partial class Order_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Appcontex app = (Appcontex)Session["app"];
            //if (app.IsAdmin == false) editor_SinglePrice.Disabled = true;
            
            string action = Request.Params.Get("Action");
            string id = Request.Params.Get("ID");
            this.pu_ID.Value = id;

          
                if (action != null && id != null)
                {
                    if (action == "Edit") { ViewDetail(int.Parse(id)); Button2121.Visible = false; };
                    // if (action == "Delete") delete_Products(int.Parse(id));
                }
            if(!IsPostBack){
                pu_ngayBan.SelectedDate = DateTime.Now;
            }
             
        }


        public void save_Order(object sender, DirectEventArgs e)
        {
            Appcontex app = (Appcontex)Session["app"];
            HeaderOrder header = new HeaderOrder();
            header.NameCus = this.pu_tenKH.Text;
            header.PhoneCus = pu_soDT.Text;
            header.Date = pu_ngayBan.SelectedDate;
            header.CountProduct = int.Parse(pu_soLuongSP.Text);
            header.TotalPrice = int.Parse(pu_tongTien.Text);
            header.TotalImportPrice = int.Parse(total_price_import.Text);
            header.AddressCus = pu_diaChi.Text;
            header.NameImployee = app.TenDangNhap;
            
            if (pu_ngayBan.IsEmpty == false) header.Date = pu_ngayBan.SelectedDate;
            else header.Date = Convert.ToDateTime("1/1/1753");

            Dictionary<string, string>[] valueGrid = JSON.Deserialize<Dictionary<string, string>[]>(e.ExtraParams["valueGrid"]);
            List<DetailOrder> listDetailOrder = new List<DetailOrder>();
           
            foreach (var value in valueGrid)
            {
                DetailOrder detail = new DetailOrder();
                foreach (KeyValuePair<string, string> keyValuePair in value)
                {
                    if (keyValuePair.Key.Equals("ID_Size") && !Formater.isNull(keyValuePair.Value))
                    {
                        detail.IdSize = int.Parse(keyValuePair.Value);
                    }
                    if (keyValuePair.Key.Equals("Size") && !Formater.isNull(keyValuePair.Value))
                    {
                        detail.Size = keyValuePair.Value;
                    }
                    if (keyValuePair.Key.Equals("ID"))
                    {
                        detail.IdProduct = int.Parse(keyValuePair.Value);
                    }
                    if (keyValuePair.Key.Equals("NameProduct"))
                    {
                        detail.NameProduct = keyValuePair.Value;
                    }
                    if (keyValuePair.Key.Equals("CodeProduct"))
                    {
                        detail.CodeProduct = keyValuePair.Value;
                    }
                    if (keyValuePair.Key.Equals("Unit"))
                    {
                        detail.Unit = keyValuePair.Value;
                    }
                    
                    if (keyValuePair.Key.Equals("SinglePrice"))
                    {
                        detail.SinglePrice = int.Parse(keyValuePair.Value);
                    }
                    if (keyValuePair.Key.Equals("Quantity"))
                    {
                        detail.Quantity = int.Parse(keyValuePair.Value);
                    }
                    if (keyValuePair.Key.Equals("TotalPrice"))
                    {
                        detail.TotalPrice = int.Parse(keyValuePair.Value);
                    }

                    

                }
                listDetailOrder.Add(detail);
            }

            new Product_Service().save_Order(header, listDetailOrder);
            MessageBox mgs = new MessageBox();
            mgs.Alert(" ", "Lưu thành công");
            mgs.Show();
        }

        public void ViewDetail(int idHeader)
        {
            HeaderOrder order = new Order_Service().GetOrder(idHeader);
            pu_tenKH.Text = order.NameCus;
            pu_soDT.Text = order.PhoneCus;
            pu_ngayBan.SelectedValue = order.Date;
            pu_diaChi.Text = order.AddressCus;
            pu_tongTien.Text = order.TotalPrice.ToString();
            pu_soLuongSP.Text = order.CountProduct.ToString();

            DS_SanPham_Store.DataSource = order.ListDetail1;
            DS_SanPham_Store.DataBind();
        }
    }
}
